param ([DataWorks.MI.Core.IItem] $Connector, [string] $EMIPath )


function Invoke-EMIImportOperation([DataWorks.MI.Core.IItem] $InSqlConnector, [string] $EMILogicalNamespacePath)
{
    $ValueBag = New-Object -TypeName "DataWorks.MI.Core.Collections.NamedValueBag"
    $ValueBag.Add("SourceFolder", [String]::Empty)
    $ValueBag.Add("ImportPublic", $True)
    $ValueBag.Add("ExcludeSystem", $False)
    $ValueBag.Add("ImportPrivate", $True)
    $ValueBag.Add("TargetFolder", [string] $EMILogicalNamespacePath)
    $InSqlConnector.InvokeWithNamedParams("ImportItems", $ValueBag)
}


function Wait-ForSyncImportOperationToComplete([int] $PollInterval, [DataWorks.MI.Core.IItem] $InSqlConnector, [int] $Timeout=600000)
{
    [int] $Duration = 0
    While ($Duration -lt $Timeout)
    {
        [System.Threading.Thread]::Sleep($PollInterval)
        [string] $LastProgress = $InSqlConnector.Invoke("GetLastSynImportProgress")
        Write-Output "Operation in Progress..."
         If ($LastProgress.StartsWith("Error: "))
         {
            Write-Output $LastProgress
            return $False
         }
         elseif ($LastProgress.EndsWith("Done."))
         {
            Write-Output "Import completed successfully."
            return $True
         }
         $Duration = $Duration + $PollInterval
    }
}

Invoke-EMIImportOperation -InSQLConnector $Connector -EMILogicalNamespacePath $EMIPath
Wait-ForSyncImportOperationToComplete -PollInterval 1000 -InSqlConnector $Connector -Timeout 600000
